<template>
  <div>
    <div>
      {{errMsg}}
    </div>
    <div>
      <el-button type="primary" @click="gotoLog">手动登录</el-button>
    </div>
  </div>

</template>

<script>
import { Loading } from 'element-ui';

let loadingInstance;
export default {
  data() {
    return {
      redirect: undefined,
      source:null,
      code:null,
      state:null,
      errMsg: null
    };
  },
  created() {

    loadingInstance = Loading.service({
      lock: true,
      text: "正在验证，请稍候",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
    })
    this.source = this.$route.query.source;
    this.code = this.$route.query.code;
    this.state = this.$route.query.state;
    this.$store.dispatch("SocialLogin", {
      code: this.code,
      state: this.state,
      source: this.source
    }).then((res) => {
      loadingInstance.close();
      //区分手机还是电脑
      if (this.isMobile()) {
       this.$router.push({ path: "/h5" }).catch(()=>{});
      } else {
        this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      }

    }).catch((err) => {
      this.errMsg = err;
      loadingInstance.close();
    });
  },
  methods: {
    gotoLog(){
      this.$router.push("/login")
    },
    isMobile() {
      const userAgent = navigator.userAgent || navigator.vendor || window.opera;
      return /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent);
    }
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
</style>
